<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/7/007
 * Time: 15:09
 */

namespace App\Http\Controllers\Oss;


if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
require_once (base_path().'/oss/samples/Common.php');
//use Illuminate\Support\Facades\Redis;
use Session,Config;
use Cache;
use Common;
class OssController extends Controller
{
    public function OssImage()
    {
        $accessKeyId = "";
        $accessKeySecret = "";
        // Endpoint以杭州为例，其它Region请按实际情况填写。
        $endpoint = "";
        // 存储空间名称
        $bucket= "";
        // 文件名称
        $object = "test";
        $content = "Hi, OSS.";
        try {
            $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
            $ossClient->putObject($bucket, $object, $content);
        } catch (OssException $e) {
            print $e->getMessage();
        }
    }

    /**
     * @param Request $request
     * @return mixed
     * 上传图片
     */
    static function uploadImage(Request $request)
    {

        $file = $request->file('wangEditorH5File');

        $originalName = $file->getClientOriginalName();
        $ext = $file->getClientOriginalExtension();//扩展名
        $type = $file->getClientMimeType();//文件类型
        $realPath = $file->getRealPath();//临时绝对路径
        
        $bucketName = Common::getBucketName();
        $ossClient = Common::getOssClient();

        if (is_null($ossClient)) exit(1);


        $imageName = 'img'. time() . '.' . $ext;

        // 先把本地的example.jpg上传到指定$bucket, 命名为$object
        $ret = $ossClient->uploadFile($bucketName, $imageName, $realPath);

        return $ret['oss-request-url'];
    }
    

}